﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;

namespace uk.ac.soton.ses
{
    public static class HDCSharedFunctions
    {
        public static List<int> GetUniqueRandomIntegers(int sizeOfArray, int maxValue, int minValue = 0,bool sort=false)
        {
            List<int> randoms = new List<int>();
            Random rndgen = new Random();
            for (int i = 0; i < sizeOfArray; i++)
            {
                int newrnd;
                do
                {
                    newrnd = rndgen.Next(minValue, maxValue);
                }
                while (randoms.Contains(newrnd));
                randoms.Add(newrnd);
            }
            if (sort)
            {
                randoms.Sort();
            }
            return randoms;
        }

        public static int? ConvertStringToNullableInt(string integerString)
        {
            try
            {
                return int.Parse(integerString);
            }
            catch (Exception ex)
            {
                if (ex is FormatException || ex is OverflowException || ex is ArgumentNullException)
                {
                    return null;
                }
                else
                {
                    throw;
                }
            }
        }

        public static bool TrySplitString(string unsplitString, char separator, int expectedParts, out string[] splitString)
        {
            splitString = new string[0];

            try
            {
                //Parse the string in the format datafileid:startrow
                splitString = unsplitString.Split(separator);

                //Skip this request as it's not in the right format
                if (splitString.Count() != expectedParts)
                {
                    return false;
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        public static bool TryParsePairString(string pairString, out string key, out int value)
        {
            key = "";
            value = 0;
            bool success = false;

            try
            {
                //Parse the string in the format datafileid:startrow
                string[] splitString;
                success = HDCSharedFunctions.TrySplitString(pairString, ':', 2, out splitString);

                //Skip this request as it's not in the right format
                if (success)
                {
                    key = splitString[0];
                    value = int.Parse(splitString[1]);
                }
            }
            catch (Exception)
            {
                success = false;
            }

            return success;
        }

        public static bool TryParsePairString(string pairString, out int key, out string value)
        {
            key = 0;
            value = "";
            bool success = false;

            try
            {
                //Parse the string in the format datafileid:startrow
                string[] splitString;
                success = HDCSharedFunctions.TrySplitString(pairString, ':', 2, out splitString);

                //Skip this request as it's not in the right format
                if (success)
                {
                    key = int.Parse(splitString[0]);
                    value = splitString[1];
                }

            }
            catch (Exception)
            {
                success = false;
            }

            return success;
        }

        public static bool TryParsePairString(string pairString, out string key, out string value)
        {
            key = "";
            value = "";
            bool success = false;

            try
            {
                //Parse the string in the format datafileid:startrow
                string[] splitString;
                success = HDCSharedFunctions.TrySplitString(pairString, ':', 2, out splitString);

                //Skip this request as it's not in the right format
                if (success)
                {
                    key = splitString[0];
                    value = splitString[1];
                }
            }
            catch (Exception)
            {
                success = false;
            }

            return success;
        }

        public static char GetDelimiterFromTextBoxString(string delimStr, char defaultDelim)
        {
            //Set the default to a tab
            char delimChar;
            if (delimStr.Length >= 1)
            {
                //If the user has entered a slash to begin with then we must unescape it to find out the character they were asking for
                //This only applies if they have entered 2 chars (e.g. \n) or more (we will just ignore the remaining chars)
                if (delimStr.Length >= 2 && delimStr[0] == '\\')
                {
                    delimStr = System.Text.RegularExpressions.Regex.Unescape(delimStr);
                }

                delimChar = delimStr[0];
            }
            else
            {
                delimChar = defaultDelim;
            }

            return delimChar;
        }


    }
}
